//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CIISLPR.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)


__fastcall TCIISLPR::TCIISLPR( TCIISDBModule *SetDB,
                               TUSBCANInterface * SetCAN,
                               TDebugWin * SetDebugWin,
                               int SetZoneNo,
                               String SetCtrl)
{

  DB = SetDB;
  CAN = SetCAN;
  DW = SetDebugWin;
  FZoneNo = SetZoneNo;
  Ctrl = SetCtrl;
  NoOpts.Clear();
  int IntervalNo;
  Byte msg[8];
  Cardinal flag;

  DB->TabZones->Locate( "ZoneNo", FZoneNo, NoOpts );
  DB->TabControllers->Locate( "CtrlName", Ctrl, NoOpts );

	ZoneCanAdr = DB->TabZones->FieldByName("ZoneCanAdr")->AsInteger;

	DB->TabSensors->Filter= "[ZoneNo] = " + IntToStr( FZoneNo );
	DB->TabSensors->Filtered= true;
	if( DB->TabSensors->FindFirst() )
	{
		FVerMajor = DB->TabSensors->FieldByName("SensorVerMajor")->AsInteger;
		FVerMinor = DB->TabSensors->FieldByName("SensorVerMinor")->AsInteger;
		do
		{
		 DB->TabSensors->Edit();
		 DB->TabSensors->FieldValues["RequestStatus"] = -1;
		 DB->TabSensors->FieldValues["SensorLPRStep"] = 0;
		 DB->TabSensors->Post();
		}
	 while( DB->TabSensors->FindNext() == true );
	}
	DB->TabSensors->Filter = "";
	DB->TabSensors->Filtered = false;
	DB->TabSensors->Refresh();


	if( FVerMajor < 2 && FVerMinor < 9 )
	{
		LPRRange = max( 1, (int)DB->TabControllers->FieldByName("LPRRange")->AsInteger );
		LPRTOn = max( 1, (int)DB->TabControllers->FieldByName("LPRDelay1")->AsInteger );
		LPRTOff = max( 1, (int)DB->TabControllers->FieldByName("LPRDelay2")->AsInteger );
		LPRStep = DB->TabControllers->FieldByName("LPRStep")->AsInteger;
		LPRMode = 0;
	}
	else
  {
		LPRRange = max( 1, (int)DB->TabControllers->FieldByName("LPRRange")->AsInteger / 10 );
		LPRTOn = max( 1, (int)DB->TabControllers->FieldByName("LPRDelay1")->AsInteger / 5 );
		LPRTOff = max( 0, (int)DB->TabControllers->FieldByName("LPRDelay2")->AsInteger / 5 );
		LPRStep = DB->TabControllers->FieldByName("LPRStep")->AsInteger / 2;
		LPRMode = DB->TabControllers->FieldByName("LPRMode")->AsInteger;
		switch (LPRMode)
		{
			case 0:
				{
				LPRStep = abs( LPRStep );
				LPRStep = max(1, min(127, LPRStep));
				}
				break;
			case 1:
			case 2:
				{
				 if( LPRStep >= 0 ) LPRStep = max(1, min(127, LPRStep));
				 else LPRStep = max( -127, LPRStep );
				 }
				break;
			case 3:
				{
				 if( LPRStep >= 0 ) LPRStep = 1;
				 else LPRStep = -1;
				}
				break;
		}
	}

	

  DB->TabRecordings->Append();
  DB->TabRecordings->FieldValues["RecNo"] = DB->TabRecordings->RecordCount + 1;
  DB->TabRecordings->FieldValues["RecStart"] = Now();
  DB->TabRecordings->FieldValues["RecType"] = 2;
  DB->TabRecordings->FieldValues["LPRRange"] = DB->TabControllers->FieldByName("LPRRange")->AsInteger;
  DB->TabRecordings->FieldValues["LPRStep"] = DB->TabControllers->FieldByName("LPRStep")->AsInteger;
  DB->TabRecordings->FieldValues["LPRDelay1"] = DB->TabControllers->FieldByName("LPRDelay1")->AsInteger;
  DB->TabRecordings->FieldValues["LPRDelay2"] = DB->TabControllers->FieldByName("LPRDelay2")->AsInteger;
  DB->TabRecordings->FieldValues["LPRMode"] = DB->TabControllers->FieldByName("LPRMode")->AsInteger;
  DB->TabRecordings->FieldValues["ZoneNo"] = FZoneNo;
  DB->TabRecordings->Post();

  FRecNo = DB->TabRecordings->FieldByName("RecNo")->AsInteger;

  DB->TabZones->Edit();
  DB->TabZones->FieldValues["RecType"] = 2;
  DB->TabZones->FieldValues["RecNo"] = FRecNo;
  DB->TabZones->Post();

	StartLPR();

  DB->Log( ClientCom, RecStart, High, "LPR", FRecNo, 0 );

}

__fastcall TCIISLPR::~TCIISLPR()
{
  Byte msg[8];
  Cardinal flag;

  DB->TabRecordings->Locate( "RecNo", FRecNo, NoOpts );
  DB->TabRecordings->Edit();
  DB->TabRecordings->FieldValues["RecStop"] = Now();
  DB->TabRecordings->Post();

  DB->Log( ClientCom, RecStop, High, "LPR", FRecNo, 0 );

  flag = canMSG_STD;
  msg[0] = 3;  // MSG_RX_UIQ_STOPLPR

  if( CAN->SendMsg( ZoneCanAdr, msg, 1, flag ) == 1 )
    #if DebugRecordings == 1
    Debug( "Request LPR Stop on CANAdr " + IntToStr(ZoneCanAdr))
    #endif
    ;
  else
    #if DebugRecordings == 1
    Debug( "TX ERR while requesting LPR Stop on CanAdr " + IntToStr(ZoneCanAdr) )
    #endif
    ;
}

void __fastcall TCIISLPR::StartLPR()
{
  Byte msg[8];
  Cardinal flag;

  flag = canMSG_STD;
  msg[0] = 1; // MSG_RX_UIQ_REQLPR
  msg[1] = LPRRange;
  msg[3] = LPRTOn;
  msg[2] = LPRTOff;
  msg[4] = LPRStep;
  msg[5] = LPRMode;

  if( CAN->SendMsg( ZoneCanAdr, msg, 6, flag ) == 6 )
    #if DebugRecordings == 1
    Debug( "Request LPR Start on CANAdr " + IntToStr(ZoneCanAdr))
    #endif
    ;
  else
    #if DebugRecordings == 1
    Debug( "TX ERR while requesting LPR Start on CanAdr " + IntToStr(ZoneCanAdr) )
    #endif
    ;

}

void __fastcall TCIISLPR::Debug(String Message)
{
  if( DW != NULL ) DW->Log(Message);
}


void __fastcall TCIISLPR::SetDebugWin(TDebugWin * SetDebugWin)
{
  DW = SetDebugWin;
}
